package cn.jhc;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ProtocolVersion;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;

public class HtmlFileRequestHandler implements HttpRequestHandler {

    @Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext context)
            throws HttpException, IOException {
        String uri = request.getRequestLine().getUri();
        String htmlFilename = uri.substring(uri.lastIndexOf("/"));
        InputStream in = getClass().getResourceAsStream(htmlFilename);
        if (in != null) {
            InputStreamEntity entity = new InputStreamEntity(in);
            response.setEntity(entity);
        } else {
            response.setStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_NOT_FOUND);
        }
    }
}
